{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,left_index=False, right_index=False, sort=True,suffixes=('_x', '_y'), copy=True)\n",
    "\n",
    "\n",
    "参数名称|说明\n",
    ":-|:-\n",
    "left/right|两个不同的 DataFrame 对象。\n",
    "on|指定用于连接的键（即列标签的名字），该键必须同时存在于左右两个 DataFrame 中，如果没有指定，并且其他参数也未指定， 那么将会以两个 DataFrame 的列名交集做为连接键。\n",
    "left_on|指定左侧 DataFrame 中作连接键的列名。该参数在左、右列标签名不相同，但表达的含义相同时非常有用。\n",
    "right_on|指定左侧 DataFrame 中作连接键的列名。\n",
    "left_index|布尔参数，默认为 False。如果为 True 则使用左侧 DataFrame 的行索引作为连接键，若 DataFrame 具有多层\n",
    "索引(MultiIndex)，则层的数量必须与连接键的数量相等。\n",
    "right_index|布尔参数，默认为 False。如果为 True 则使用左侧 DataFrame 的行索引作为连接键。\n",
    "how|要执行的合并类型，从 {'left', 'right', 'outer', 'inner'} 中取值，默认为“inner”内连接。\n",
    "sort|布尔值参数，默认为True，它会将合并后的数据进行排序；若设置为 False，则按照 how 给定的参数值进行排序。\n",
    "suffixes|字符串组成的元组。当左右 DataFrame 存在相同列名时，通过该参数可以在相同的列名后附加后缀名，默认为('_x','_y')。\n",
    "copy|默认为 True，表示对数据进行复制。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  id         A         B         C         D         E         F\n",
      "一  0  0.922099  0.631717  0.289842  0.949366  0.529917  0.071891\n",
      "二  1  0.053323  0.807510  0.320216  0.515494  0.884284  0.537018\n",
      "三  2  0.578609  0.271367  0.772499  0.475112  0.235377  0.633867\n",
      "四  3  0.279314  0.057593  0.634263  0.718388  0.262428  0.646105\n",
      "五  4  0.395188  0.263144  0.866434  0.560993  0.590829  0.151182\n",
      "六  5  0.306482  0.212830  0.131054  0.603515  0.846955  0.457590\n",
      "七  6  0.732032  0.003241  0.540454  0.874635  0.988549  0.917909\n",
      "八  7  0.161961  0.228617  0.929486  0.085531  0.598552  0.815687\n",
      "九  8  0.517293  0.211741  0.061705  0.543238  0.875000  0.294196\n",
      "十  9  0.619896  0.333039  0.311582  0.099216  0.510971  0.550156\n"
     ]
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    \"id\": list(\"0123456789\"),\n",
    "    \"A\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"B\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"C\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"D\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"E\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"F\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\"))\n",
    "})\n",
    "\n",
    "df2 = pd.DataFrame({\n",
    "    \"id\": list(\"0123456789\"),\n",
    "    \"G\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"H\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"I\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"J\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"K\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\")),\n",
    "    \"L\":pd.Series(np.random.rand(10),index=list(\"一二三四五六七八九十\"))\n",
    "})\n",
    "\n",
    "print(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "      <th>G</th>\n",
       "      <th>H</th>\n",
       "      <th>I</th>\n",
       "      <th>J</th>\n",
       "      <th>K</th>\n",
       "      <th>L</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.922099</td>\n",
       "      <td>0.631717</td>\n",
       "      <td>0.289842</td>\n",
       "      <td>0.949366</td>\n",
       "      <td>0.529917</td>\n",
       "      <td>0.071891</td>\n",
       "      <td>0.681902</td>\n",
       "      <td>0.565884</td>\n",
       "      <td>0.852017</td>\n",
       "      <td>0.612172</td>\n",
       "      <td>0.739586</td>\n",
       "      <td>0.407426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.053323</td>\n",
       "      <td>0.807510</td>\n",
       "      <td>0.320216</td>\n",
       "      <td>0.515494</td>\n",
       "      <td>0.884284</td>\n",
       "      <td>0.537018</td>\n",
       "      <td>0.897127</td>\n",
       "      <td>0.397460</td>\n",
       "      <td>0.730548</td>\n",
       "      <td>0.419410</td>\n",
       "      <td>0.257468</td>\n",
       "      <td>0.824182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.578609</td>\n",
       "      <td>0.271367</td>\n",
       "      <td>0.772499</td>\n",
       "      <td>0.475112</td>\n",
       "      <td>0.235377</td>\n",
       "      <td>0.633867</td>\n",
       "      <td>0.724279</td>\n",
       "      <td>0.644371</td>\n",
       "      <td>0.758787</td>\n",
       "      <td>0.655025</td>\n",
       "      <td>0.053778</td>\n",
       "      <td>0.758712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.279314</td>\n",
       "      <td>0.057593</td>\n",
       "      <td>0.634263</td>\n",
       "      <td>0.718388</td>\n",
       "      <td>0.262428</td>\n",
       "      <td>0.646105</td>\n",
       "      <td>0.957251</td>\n",
       "      <td>0.392182</td>\n",
       "      <td>0.358706</td>\n",
       "      <td>0.600604</td>\n",
       "      <td>0.503081</td>\n",
       "      <td>0.045716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.395188</td>\n",
       "      <td>0.263144</td>\n",
       "      <td>0.866434</td>\n",
       "      <td>0.560993</td>\n",
       "      <td>0.590829</td>\n",
       "      <td>0.151182</td>\n",
       "      <td>0.191865</td>\n",
       "      <td>0.153609</td>\n",
       "      <td>0.145687</td>\n",
       "      <td>0.661889</td>\n",
       "      <td>0.505479</td>\n",
       "      <td>0.893584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>0.306482</td>\n",
       "      <td>0.212830</td>\n",
       "      <td>0.131054</td>\n",
       "      <td>0.603515</td>\n",
       "      <td>0.846955</td>\n",
       "      <td>0.457590</td>\n",
       "      <td>0.783668</td>\n",
       "      <td>0.451863</td>\n",
       "      <td>0.648520</td>\n",
       "      <td>0.384921</td>\n",
       "      <td>0.739583</td>\n",
       "      <td>0.350502</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>0.732032</td>\n",
       "      <td>0.003241</td>\n",
       "      <td>0.540454</td>\n",
       "      <td>0.874635</td>\n",
       "      <td>0.988549</td>\n",
       "      <td>0.917909</td>\n",
       "      <td>0.334894</td>\n",
       "      <td>0.433113</td>\n",
       "      <td>0.380587</td>\n",
       "      <td>0.037942</td>\n",
       "      <td>0.658735</td>\n",
       "      <td>0.944182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>0.161961</td>\n",
       "      <td>0.228617</td>\n",
       "      <td>0.929486</td>\n",
       "      <td>0.085531</td>\n",
       "      <td>0.598552</td>\n",
       "      <td>0.815687</td>\n",
       "      <td>0.835598</td>\n",
       "      <td>0.888580</td>\n",
       "      <td>0.679148</td>\n",
       "      <td>0.878820</td>\n",
       "      <td>0.245809</td>\n",
       "      <td>0.005209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>0.517293</td>\n",
       "      <td>0.211741</td>\n",
       "      <td>0.061705</td>\n",
       "      <td>0.543238</td>\n",
       "      <td>0.875000</td>\n",
       "      <td>0.294196</td>\n",
       "      <td>0.634610</td>\n",
       "      <td>0.138164</td>\n",
       "      <td>0.592620</td>\n",
       "      <td>0.524579</td>\n",
       "      <td>0.234848</td>\n",
       "      <td>0.443304</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>0.619896</td>\n",
       "      <td>0.333039</td>\n",
       "      <td>0.311582</td>\n",
       "      <td>0.099216</td>\n",
       "      <td>0.510971</td>\n",
       "      <td>0.550156</td>\n",
       "      <td>0.460897</td>\n",
       "      <td>0.705222</td>\n",
       "      <td>0.947592</td>\n",
       "      <td>0.070710</td>\n",
       "      <td>0.327650</td>\n",
       "      <td>0.456627</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  id         A         B         C         D         E         F         G  \\\n",
       "0  0  0.922099  0.631717  0.289842  0.949366  0.529917  0.071891  0.681902   \n",
       "1  1  0.053323  0.807510  0.320216  0.515494  0.884284  0.537018  0.897127   \n",
       "2  2  0.578609  0.271367  0.772499  0.475112  0.235377  0.633867  0.724279   \n",
       "3  3  0.279314  0.057593  0.634263  0.718388  0.262428  0.646105  0.957251   \n",
       "4  4  0.395188  0.263144  0.866434  0.560993  0.590829  0.151182  0.191865   \n",
       "5  5  0.306482  0.212830  0.131054  0.603515  0.846955  0.457590  0.783668   \n",
       "6  6  0.732032  0.003241  0.540454  0.874635  0.988549  0.917909  0.334894   \n",
       "7  7  0.161961  0.228617  0.929486  0.085531  0.598552  0.815687  0.835598   \n",
       "8  8  0.517293  0.211741  0.061705  0.543238  0.875000  0.294196  0.634610   \n",
       "9  9  0.619896  0.333039  0.311582  0.099216  0.510971  0.550156  0.460897   \n",
       "\n",
       "          H         I         J         K         L  \n",
       "0  0.565884  0.852017  0.612172  0.739586  0.407426  \n",
       "1  0.397460  0.730548  0.419410  0.257468  0.824182  \n",
       "2  0.644371  0.758787  0.655025  0.053778  0.758712  \n",
       "3  0.392182  0.358706  0.600604  0.503081  0.045716  \n",
       "4  0.153609  0.145687  0.661889  0.505479  0.893584  \n",
       "5  0.451863  0.648520  0.384921  0.739583  0.350502  \n",
       "6  0.433113  0.380587  0.037942  0.658735  0.944182  \n",
       "7  0.888580  0.679148  0.878820  0.245809  0.005209  \n",
       "8  0.138164  0.592620  0.524579  0.234848  0.443304  \n",
       "9  0.705222  0.947592  0.070710  0.327650  0.456627  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df1,df2,on=[\"id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
